cli-plugins: separate hook types from manager and refactor#6859
cli-plugins: separate hook types from manager and refactor#6859thaJeztah wants to merge 15 commits intodocker:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Currently, the error was a plain "exit status 1"; make the error message more informative if we need it :) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Extract the code inside the loop to a closure, so that we can more easily set up debug-logging. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Separate types used by plugins from the manager code. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Rename the type to match the struct it's used for. Also; - Fix the type of the NextSteps const - Don't use iota for values; the ResponseType is used as part of the "wire" format, which means that plugins using the value can use a different version of the module code; using iota increases the risk of (accidentally) changing values, which would break the wire format. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
9b06ef5 to
a2448d5
Compare
cli-plugins/hooks/template.go
Outdated
| if n := strings.Count(out, "\n"); n > maxMessages { | ||
| return nil, fmt.Errorf("hook template contains too many messages (%d): maximum is %d", n, maxMessages) | ||
| } | ||
| return strings.SplitN(out, "\n", maxMessages), nil |
There was a problem hiding this comment.
10 newlines can be 11 messages
Should we do strings.SplitN(out, "\n", maxMessages+1) and check the length of the resulting slice?
There was a problem hiding this comment.
We should probably have a test for that
There was a problem hiding this comment.
Ah, yes, I was already considering leaving this one for a follow-up; its was mostly to prevent garbage resulting in a lot of garbage; I just dropped the commit from this PR for now
| // RootCmd is a string representing the matching hook configuration | ||
| // which is currently being invoked. If a hook for "docker context" | ||
| // is configured and the user executes "docker context ls", the plugin | ||
| // is invoked with "context". | ||
| RootCmd string `json:"rootCmd,omitzero"` | ||
|
|
||
| // Flags contains flags that were set on the command for which the | ||
| // hook was invoked. It uses flag names as key, with leading hyphens | ||
| // removed ("--flag" and "-flag" are included as "flag" and "f"). | ||
| // | ||
| // Flag values are not included and are set to an empty string, | ||
| // except for boolean flags known to the CLI itself, for which | ||
| // the value is either "true", or "false". | ||
| // | ||
| // Plugins can use this information to adjust their [Response] | ||
| // based on whether the command triggering the hook was invoked | ||
| // with. | ||
| Flags map[string]string `json:"flags,omitzero"` | ||
|
|
||
| // CommandError is a string containing the error output (if any) | ||
| // of the command for which the hook was invoked. | ||
| CommandError string `json:"commandError,omitzero"` |
There was a problem hiding this comment.
Previously these were using the default JSON casing so (RootCmd not rootCmd).
Can we break the format here?
There was a problem hiding this comment.
no, shouldn't ... really; when parsing, Golang is still case-insensitive, but if there's multiple fields only differing in casing, it will prefer the one added here.
a2448d5 to
afe84d6
Compare
Add labels to define the expected casing and don't serialize empty fields. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
These utilities are used by CLI-plugins; separate them from the render code, which is used by teh CLI-plugin manager. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- add basic unit-test for the template utilities
- make sure the template parsing tests test both the current
template produced by the utilities, as well as a fixture
- rewrite the printer test to use fixtures
- use blackbox testing ("hooks_test")
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- use `%q` instead of manually quoting the string - use `%d` instead of manually converting the number to a string Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
afe84d6 to
69ab970
Compare
Define a local type for methods to expose to the template, instead of passing the cobra.Cmd. This avoids templates depending on features exposed by Cobra that are not part of the contract, and slightly decouples the templat from the Cobra implementation. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This allows for slighly cleaner / more natural placeholders, as it
doesn't require the context (`.`) to be specified;
- `{{command}}` instead of `{{.Command}}` or `{{command .}}`
- `{{flagValue "my-flag"}}` instead of `{{.FlagValue "my-flag"}} or `{{flagValue . "my-flag"}}`
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- skip aec to construct the formatting and use a const instead - skip fmt.Println and write directly to the writer Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
69ab970 to
bbc89a3
Compare
cli-plugins/manager: Plugin.RunHook: improve error message
Currently, the error was a plain "exit status 1"; make the error
message more informative if we need it :)
cli-plugins/manager: simplify ctx-cancel check
cli-plugins/manager: refactor for easier debugging
Extract the code inside the loop to a closure, so that we can more
easily set up debug-logging.
cli-plugins/manager: move HookPluginData to hooks.Request
Separate types used by plugins from the manager code.
cli-plugins/hooks: rename HookMessage to Response
cli-plugins/hooks: rename HookType to ResponseType
Rename the type to match the struct it's used for. Also;
part of the "wire" format, which means that plugins using
the value can use a different version of the module code;
using iota increases the risk of (accidentally) changing
values, which would break the wire format.
cli-plugins/hooks: add JSON labels, omitzero
Add labels to define the expected casing and don't serialize
empty fields.
cli-plugins/hooks: move template utils separate from render code
These utilities are used by CLI-plugins; separate them from the render
code, which is used by teh CLI-plugin manager.
cli-plugins/hooks: update tests
template produced by the utilities, as well as a fixture
cli-plugins/hooks: slight tweaks in templates
%qinstead of manually quoting the string%dinstead of manually converting the number to a stringcli-plugins/hooks: detect if templating is needed
cli-plugins/hooks: limit maximum number of lines / messages
cli-plugins/hooks: update godoc
cli-plugins/hooks: add commandInfo type for templating
Define a local type for methods to expose to the template, instead of
passing the cobra.Cmd. This avoids templates depending on features
exposed by Cobra that are not part of the contract, and slightly
decouples the templat from the Cobra implementation.
cli-plugins/hooks: simplify templating formats
This allows for slighly cleaner / more natural placeholders, as it
doesn't require the context (
.) to be specified;{{command}}instead of{{.Command}}or{{command .}}{{flagValue "my-flag"}}instead of{{.FlagValue "my-flag"}} or{{flagValue . "my-flag"}}`cli-plugins/hooks: PrintNextSteps: slight cleanup
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)